//index.js
let logger = require('../../utils/logger')
var app = getApp()

Page({
  data: {
    token: '',
    cityName: '',
    cityTitle: '',
    msgCount: 0,
    isShowMsg: true,
    subjects: [],
    shareTitle: '',
    shareId: '',
    subjectTypes: [],
    subjectTypeId: -1,
    pageIndex: 1,
    pageSize: 10,
    hasNext: true,
    loading: false
    
  },
  onLoad: function () {
  },
  onShow: function () {
  },
  onReachBottom: function (e) {
    if (!this.data.hasNext) {
      return
    }
    this.getSubject()
  },
  onShareAppMessage: function (e) {
    let subject = e.target.dataset.subject
    
    let opt = {
      success: function (res) {
        logger.info('分享成功', res)
      }
    }
    if (subject) {
      opt.title = subject.detail.substring(0, 30)
      opt.path = 'pages/subject/pages/detail/detail?id=' + subject.id
      if (subject.picFileUrlList) {
        opt.imageUrl = subject.picFileUrlList[0]
      }
    }
    
    return opt
  },
  init () {
    this.setData({
      pageIndex: 1
    })
    this.getSubject()
    this.getUnReadCount()
    this.getAllSubjectType()
  },
  getUnReadCount () {
    let that = this
    app.http.get({
      path: '/msg/queryUnReadTotal',
      success (res) {
        that.setData({
          msgCount: res | 0
        })
      }
    })
  },
  getSubject (){
    if (this.data.loading) {
      return
    }
    
    let sortType = ''
    
    if (this.data.subjectTypeId == -1) {
      sortType = 'createTime'
    } else if (this.data.subjectTypeId == -2) {
      sortType = 'viewNum'
    }
    
    this.setData({
      loading: true
    })
    let that = this
    app.http.get({
      path: '/subject/queryPageListByParam',
      data: {
        subjectTypeId: this.data.subjectTypeId < 0 ? '' : this.data.subjectTypeId,
        pageIndex: this.data.pageIndex,
        pageSize: this.data.pageSize,
        sortType: sortType
      },
      success (res) {
        let data = res.data
        let subjects = []
        let pageIndex = that.data.pageIndex
        // 如果不是第1页， 塞入原数据
        if (pageIndex > 1) {
          subjects = that.data.subjects
        }
        
        data.forEach(item => {
          if (item.detail.length > 60) {
            item.formerDetail = item.detail.substring(0, 60)
            item.hasMore = true
          } else {
            item.formerDetail = item.detail
            item.hasMore = false
          }
          subjects.push(item)
        })
        
        that.setData({
          subjects: subjects,
          pageIndex: pageIndex + 1,
          hasNext: res.hasNext,
          loading: false
        })
      },
      fail () {
        that.setData({
          hasNext: true,
          loading: false
        })
      }
    })
  },
  getAllSubjectType () {
    let that = this
    app.http.get({
      path: '/subjectType/queryAllSubjectType',
      success (res) {
        that.setData({
          subjectTypes: res
        })
      }
    })
  },
  // 预览图片
  onPreviewPic (e) {
    let pics = e.target.dataset.pics
    let currentPic = e.target.dataset.currentPic
    if (!pics || pics.length === 0) {
      return
    }
    if (!currentPic) {
      currentPic = pics[0]
    }
    
    wx.previewImage({
      current: currentPic, // 当前显示图片的http链接
      urls: pics // 需要预览的图片http链接列表
    })
  },
  onSelectType (e) {
    let id = e.target.dataset.id
    this.setData({
      subjectTypeId: id,
      pageIndex: 1
    })
    this.getSubject()
  },
  onPraise (e) {
    let subject = e.target.dataset.subject
    if (subject.hasPraise) {
      return
    }
    
    let that = this
    app.http.get({
      path: '/cp/commonPraise',
      data: {
        type: 'subject',
        objectId: subject.id,
        praiseType: 'add'
      },
      success (res) {
        if (res > 0) {
          let subjects = that.data.subjects
          subjects.forEach(item => {
            if (item.id == subject.id) {
              item.hasPraise = true
              item.praiseNum = item.praiseNum + 1
            }
          })
          that.setData({
            subjects: subjects
          })
        }
      }
    })
  },
  newSubject () {
    wx.navigateTo({
      url: 'pages/edit/edit'
    })
  }
})
